package org.example.commerce.imp.controller;


import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.example.commerce.api.beans.CreateNewGoodsParam;
import org.example.commerce.api.beans.ModifyGoodsParam;
import org.example.commerce.dao.entity.Goods;
import org.example.commerce.dao.vo.ProduceWithCategoryVo;
import org.example.commerce.imp.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/good")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

//  请求所有商品数据
    @GetMapping("/{page}/{pageSize}")
    public PageInfo<ProduceWithCategoryVo> getAllGoods(@PathVariable Integer page, @PathVariable Integer pageSize){
        log.info("get all goods page={} pageSize={}",page,pageSize);
        return goodsService.selectAll(page,pageSize);
    }

//    查询单个id数据
    @GetMapping("/{id}")
    public Goods getGoodsById(@PathVariable Long id){
        log.info("Delete drink, id={}", id);
        return goodsService.selectGoodsById(id);
    }

//    创建商品
    @PostMapping("/")
    public Goods addGoods(@Valid @RequestBody CreateNewGoodsParam request){
        log.info("Create goods: data={}", request);
        return goodsService.addGoods(request);
    }

//  编辑商品
    @PutMapping("/")
    public Goods updateGoods(@Valid @RequestBody ModifyGoodsParam request){
        log.info("update goods: data={}", request);
        return goodsService.updateGoods(request);
    }

//    删除商品
    @DeleteMapping("/{id}")
    public String deleteGoodsById(@PathVariable Long id){
        log.info("Delete drink, id={}", id);
        goodsService.deleteGoodsById(id);
        return "删除成功";
    }
}
